<template>
  <div>
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.teamName" placeholder="代表队名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.realName" placeholder="选手姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.ticket" placeholder="选手编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.company" placeholder="单位" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="选择状态">
          <el-option label="全部" value=""></el-option>
          <el-option label="审核中" value="0"></el-option>
          <el-option label="报名成功" value="1"></el-option>
          <el-option label="完赛" value="2"></el-option>
          <el-option label="已取消/弃权" value="3"></el-option>
          <el-option label="比赛中" value="4"></el-option>
          <el-option label="已获晋级赛资格" value="5"></el-option>
          <el-option label="晋级成功" value="6"></el-option>
          <el-option label="弃权" value="7"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地区" prop="province">
        <el-select style="width: 150px" placeholder="请选择" v-model="dataForm.province" @change="selectCity">
          <el-option value="" label="全国">全国</el-option>
          <el-option v-for="itemO in area.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
        ~
        <el-select style="width: 150px" placeholder="请选择" v-model="dataForm.city">
          <el-option value="" label="全部">全部</el-option>
          <el-option v-for="itemO in areaCity.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-tag size="big">{{ matchItem ? matchItem.match_name : "" }}</el-tag>
        <el-tag size="big" style="margin-left: 10px">{{ matchItem ? matchItem.item_name : "" }}</el-tag>
        <el-tag size="big" style="margin-left: 10px">{{ dataForm.groupNameFile ? dataForm.groupNameFile : "" }}</el-tag>
      </el-form-item>
    </el-form>
    <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" @sort-change="dataListSortChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" width="80" label="序号" sortable="custom" header-align="center" align="center"></el-table-column>
      <el-table-column prop="sort" width="120" label="抽签结果" header-align="center" align="center" v-if="dataForm.drawOpen == 1 && dataForm.isDraw == 1"></el-table-column>
      <el-table-column prop="teamName" width="200" label="代表队名称" header-align="center" align="center">
        <template v-slot="scope">
          <el-button type="text" @click="teamHandle(scope.row.teamId)">{{ scope.row.teamName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="company" width="200" label="选手单位" header-align="center" align="center"></el-table-column>
      <el-table-column prop="realName" width="120" label="选手" header-align="center" align="center">
        <template v-slot="scope">
          <el-button type="text" @click="userHandle(scope.row.userId)">{{ scope.row.realName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="ticket" width="150" label="选手编号" header-align="center" align="center">
        <template v-slot="scope">
          <el-button type="text" @click="toScore(scope.row.id)">{{ scope.row.ticket }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="teacher" width="150" label="指导老师" header-align="center" align="center"></el-table-column>
      <el-table-column prop="score" v-if="scores[0]" width="110" :label="$t(scores[0])" header-align="center" align="center">
        <template v-slot="scope">
          {{scope.row.score}}{{(scope.row.score00 && scope.row.score00!='null') ? '/' + scope.row.score00 : ''}}
        </template>
      </el-table-column>
      <el-table-column prop="score1" v-if="scores[1]" width="110" :label="$t(scores[1])" header-align="center" align="center">
        <template v-slot="scope">
          {{scope.row.score1}}{{(scope.row.score11 && scope.row.score11!='null') ? '/' + scope.row.score11 : ''}}
        </template>
      </el-table-column>
      <el-table-column prop="score2" v-if="scores[2]" width="100" :label="$t(scores[2])" header-align="center" align="center"></el-table-column>
      <el-table-column prop="ranks" width="100" label="名次" sortable="custom" header-align="center" align="center"></el-table-column>
      <el-table-column prop="grade" width="100" label="奖等" header-align="center" align="center"></el-table-column>
      <el-table-column prop="certificate" width="200" label="证书编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="status" width="120" label="状态" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="">审核中</el-tag>
          <el-tag v-else-if="scope.row.status === 1" size="small" type="">报名成功</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="small" type="">完赛</el-tag>
          <el-tag v-else-if="scope.row.status === 3" size="small" type="">已取消/弃权</el-tag>
          <el-tag v-else-if="scope.row.status === 4" size="small" type="">比赛中</el-tag>
          <el-tag v-else-if="scope.row.status === 5" size="small" type="">已获晋级赛资格</el-tag>
          <el-tag v-else-if="scope.row.status === 6" size="small" type="">晋级成功</el-tag>
          <el-tag v-else-if="scope.row.status === 7" size="small" type="">弃权</el-tag>
          <el-tag v-else-if="scope.row.status === 8" size="small" type="">驳回</el-tag>
          <el-tag v-else-if="scope.row.status === 9" size="small" type="">待确认</el-tag>
          <el-tag v-else-if="scope.row.status === 10" size="small" type="">报名已截止</el-tag>
          <el-tag v-else size="small" type="">其他</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" width="200" label="备注" header-align="center" align="center"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button type="text" size="small" v-if="
          (
            matchItem.item_name === '个人飞行赛' ||
            matchItem.item_name === '装调物流搬运赛' ||
            matchItem.item_name === '编程挑战赛'
          )
          && hasPermission('sp:spmatchitemrecord:scoring')"
                     @click="toScore(scope.row.id, matchItem.item_name)">打分</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"> </el-pagination>
    <TeamDetail v-if="teamVisible" ref="teamRef" @refreshDataList="getDataList"></TeamDetail>
    <detail v-if="userVisible" ref="userRef" @refreshDataList="getDataList"></detail>
    <!--打分工具-->
    <el-dialog v-model="scoreVisible" title="打分工具" :close-on-click-modal="false" :close-on-press-escape="false" :width="dialogWidth">
      <!--
      <iframe :src="'http://login.qdmay.top/#/autoGrade/scoring?id=' + scoreId" :width="frameWidth" height="500" style="border: none;"></iframe>
      -->
      <iframe :src="iframeUrl" ref="scoreRef" :width="frameWidth" :key="keys" height="550" style="border: none"></iframe>

      <template v-slot:footer>
        <el-button @click="scoreVisible = false" type="danger">关闭</el-button>
      </template>

      <!--<ren-scoring ></ren-scoring>-->
    </el-dialog>
  </div>
</template>
<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import baseService from "@/service/baseService";
import TeamDetail from "@/views/sp/spteam-detail.vue";
import Detail from "@/views/sp/spuser-detail.vue";
import {getDictDataList} from "@/utils/utils";
import { useStore } from "vuex";
export default defineComponent({
  name: "RenToolStudent",
  components: {
    TeamDetail,
    Detail
  },
  setup() {
    const store = useStore();
    const state = reactive({
      getDataListURL: "/sp/spmatchitemteam/signRecordStudent",
      createdIsNeed: false,
      activatedIsNeed: false,
      getDataListIsPage: true,
      exportURL: "/sp/spmatchitemteam/exportStudent",
      deleteURL: "/sp/spmatchitemteam",
      deleteIsBatch: true,
      dataForm: {
        matchName: "",
        item: "",
        matchItemId: "",
        teamId: "",
        teamName: "",
        isRise: "",
        status: "",
        company: "",
        realName: "",
        groupId: "",
        groupNameFile: "",
        province: "",
        city: "",
        fileName: "",
        drawOpen: null,
        isDraw: null,
        matchItemTeamUserId: "",
        itemType: "",
        ticket: ""
      },
      dialogWidth: "1270px",
      frameWidth: "98%",
      activeName: "first",
      area: { code: "0", item: [{ code: "0", name: "请选择" }] },
      areaCity: { code: "0", item: [{ code: "0", name: "请选择" }] },
      matchItem: {
        match_name: "",
        item_name: ""
      },
      teamVisible: false,
      userVisible: false,
      scoreVisible: false,
      scores: [],
      scoreId: "",
      iframeUrl: "",
      keys: "" as any,
      programScores: null,
      storeObj: store
    });
    return { ...useView(state), ...toRefs(state) };
  },
  props: {
    itemType: String
  },
  watch: {},
  created() {
    this.areaList(this.area);
  },
  mounted() {
    //获取上个页面传来的参数
    this.dataForm.matchItemId = this.$route.query.matchItemId + "";
    this.dataForm.groupId = this.$route.query.id + "";
    this.dataForm.groupNameFile = this.$route.query.groupName + "";
    this.dataForm.drawOpen = this.$route.query.drawOpen + "";
    this.dataForm.isDraw = this.$route.query.isDraw + "";
    this.dataForm.itemType = this.$route.query.itemType + "";
    //this.queryBaseMatchItem();
    this.query();
    this.queryBymatchItemId(this.dataForm.matchItemId);
    if(this.$route.query.groupName==='小学组'){
      debugger
      this.programScores = getDictDataList(this.storeObj.state.dicts, "program-x")
      console.log(this.programScores);
    }
    if(this.$route.query.groupName==='初中组'){
      this.programScores = getDictDataList(this.storeObj.state.dicts, "program-c")
    }
    if(this.$route.query.groupName==='高中组'){
      this.programScores = getDictDataList(this.storeObj.state.dicts, "program-g")
    }

    // 监听打分工具传来的数据
    window.addEventListener("message", this.reloadData);
  },
  methods: {
    /**
     * 父页面方法，由打分工具触发
     * @param target
     */
    reloadData() {
      // 关闭弹窗
      this.scoreVisible = false;
      // 刷新数据
      this.query();
    },

    /**
     * 获取省份
     * @param target
     */
    areaList(target: any) {
      baseService.get("/front/fast/sys/areaDict/list?pid=" + target.code).then((e: any) => {
        target.item = e.data;
      });
    },

    /**
     * 选择城市
     */
    selectCity() {
      if (this.dataForm.province) {
        this.areaCity.code = this.dataForm.province;
        this.areaList(this.areaCity);
      } else {
        this.dataForm.city = "";
        this.areaCity.item = [];
      }
    },

    /**
     * 查询score配置
     * @param matchItemId
     */
    queryBymatchItemId(matchItemId: any) {
      baseService.post("/sp/spmatchitem/queryBymatchItemId", { id: matchItemId }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        } else {
          console.log(res.data);
          this.matchItem = res.data;
          this.dataForm.fileName = this.matchItem.item_name + "(" + this.dataForm.groupNameFile + ")";
          this.scores = res.data.scores.split(",");
        }
      });
    },

    /**
     * 代表队详情
     * @param id
     */
    teamHandle(id?: string) {
      this.teamVisible = true;
      baseService.get("/sp/spteam/" + id).then((e: any) => {
        this.$nextTick(() => {
          if (e.data) {
            e.data.sex = e.data.sex == 0 ? "女" : "男";
          }
          this.$refs.teamRef.dataForm.row = e.data;
          this.$refs.teamRef.init();
        });
      });
    },

    /**
     * 用户详情
     * @param id
     */
    userHandle(id?: string) {
      this.userVisible = true;
      baseService.get("/sp/spuser/" + id).then((e: any) => {
        this.$nextTick(() => {
          if (e.data) {
            e.data.sex = e.data.sex == 0 ? "女" : "男";
          }
          this.$refs.userRef.dataForm.row = e.data;
          this.$refs.userRef.init();
        });
      });
    },

    /**
     * 打分
     * @param id
     */
    toScore(id?: string, item?: string) {
      this.keys = new Date().getTime();
      this.scoreId = id;
      this.scoreVisible = true;
      // const baseUrl = 'http://localhost:8080/#/autoGrade/';
      // const baseUrl = 'http://login.qdmay.top/#/autoGrade/';
      const baseUrl = 'http://login.int-ede.com/#/autoGrade/';

      const r = '&r=5';

      if(item === '个人飞行赛') {
        this.iframeUrl = baseUrl + 'scoring?id=' + id + r;
      }
      if(item === '装调物流搬运赛') {
        this.iframeUrl = baseUrl + 'logisticsHandlingScoring?id=' + id + r;
      }
      if(item === '编程挑战赛') {
        // 获取store
        console.log(this.programScores);
        let dis = '';
        for(let i = 0; i < this.programScores.length; i++) {
          dis += this.programScores[i].dictValue + ',';
        }
        //去掉最后的逗号
        if(dis.length > 0){
          dis = dis.substring(0, dis.length - 1);
        }
        this.iframeUrl = baseUrl + 'programScoring?id=' + id + '&dis=' + dis + '&groupId=' + this.dataForm.groupId + r;
      }

    }
  }
});
</script>
<style>
::v-deep .el-dialog {
  height: 78vh;
  overflow: auto;
}
::v-deep .el-dialog {
  height: 78vh;
  overflow: auto;
}
</style>
